package com.hzdl.library.dao.impl;

import com.hzdl.library.dao.SelectUserByOneDao;
import com.hzdl.library.domain.ReaderCard;
import com.hzdl.library.domain.ReaderInfo;
import com.hzdl.library.util.JDBCUtils;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

//jm
public class SelectUserByOneDaoImpl implements SelectUserByOneDao {

    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public Integer selectByOne(String id) {
        String sql = "select count(*) from reader_info where reader_id = ?";
        Integer integer = template.queryForObject(sql, Integer.class, id);
        return integer;
    }

    @Override
    public ReaderCard selectReaderCard(Integer id, String passwd) {
        String sql = "select * from reader_card where reader_id = ? and passwd = ?";
        try {
            return template.queryForObject(sql,new BeanPropertyRowMapper<ReaderCard>(ReaderCard.class),id,passwd);
        } catch (DataAccessException e) {
//            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ReaderInfo selectReader(Integer id) {
        String sql = "select * from reader_info where reader_id = ?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<ReaderInfo>(ReaderInfo.class), id);
        } catch (DataAccessException e) {
//            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Integer updateReaderCard(Integer id, String newPass) {
        String sql = "update reader_card set passwd = ? where reader_id = ?";
        return template.update(sql,newPass,id);
    }

}
